package com.hrms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrms.pojo.AttenManage;
import com.hrms.dao.AttenManageMapper;
import com.hrms.service.AttenManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.HashMap;

/**
 * 人事调配记录表 服务实现类
 *
 * @author LK
 * @since 2021-10-19
 */
@Service
public class AttenManageServiceImpl  implements AttenManageService {

	@Autowired(required = false)
	AttenManageMapper attenManageMapper;
	@Override
	public HashMap<String, Object> select(AttenManage a) {
		HashMap<String, Object> map = new HashMap<>();
		//定义分页对象
		Page<AttenManage> page = new Page<>(a.getPage(), a.getRow());
		QueryWrapper<AttenManage> queryWrapper = new QueryWrapper<>();
		//编号查询
		queryWrapper.eq(a.getStaffId() != null, "staff_id", a.getStaffId());
		//Page<AttenManage> aList = attenManageMapper.selectByCon(page, a);

		//用户名的模糊查询
		queryWrapper.like(!StringUtils.isEmpty(a.getUserName()) && a.getUserName() != null, "user_name",
				a.getUserName());
		//查询数据库
		Page<AttenManage> aList =attenManageMapper.selectPage(page,queryWrapper);
		//当前页集合
		map.put("list",aList.getRecords());
		//总页数
		map.put("totalPage",aList.getPages());
		//总条数
		map.put("total",aList.getTotal());
		//当前页
		map.put("curPage",a.getPage());
		if(a.getPage()==1){
			//上一页
			map.put("prePage",1);

		}else{
			map.put("prePage",a.getPage()-1);

		}
		if(a.getPage()==aList.getPages()){
			//下一页
			map.put("nextPage",aList.getPages());

		}else{
			map.put("nextPage",a.getPage()+1);

		}
		//每页显示条数
		map.put("row",a.getRow());

		return map;
	}

	@Override
	public HashMap<String, Object> add(AttenManage a) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		//

		//新增
		int num = attenManageMapper.insert(a);
		if(num>0){
			map.put("info","保存成功");
		}else{
			map.put("info","保存错误");
		}
		return map;
	}

	@Override
	public HashMap<String, Object> update(AttenManage a) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		//修改
		int num = attenManageMapper.updateById(a);
		if(num>0){
			map.put("info","保存成功");
		}else{
			map.put("info","保存错误");
		}
		return map;
	}

	@Override
	public HashMap<String, Object> del(Integer a) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		//删除
		int num = attenManageMapper.deleteById(a);
		if(num>0){
			map.put("info","保存成功");
		}else{
			map.put("info","保存错误");
		}
		return map;
	}
}
